package ik.xcj.hostpital.manage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ik.xcj.hostpital.manage.arithmetic.MD5;
import ik.xcj.hostpital.manage.ex.ServiceException;
import ik.xcj.hostpital.manage.mapper.HospitalSetMapper;
import ik.xcj.hostpital.manage.mapper.IHospitalMapper;
import ik.xcj.hostpital.manage.pojo.dto.HospitalSetAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.HospitalSetUpdateDTO;
import ik.xcj.hostpital.manage.pojo.entity.HospitalSet;
import ik.xcj.hostpital.manage.pojo.vo.HospitalSetQVO;
import ik.xcj.hostpital.manage.pojo.vo.HospitalSetVO;
import ik.xcj.hostpital.manage.pojo.vo.HospitalVO;
import ik.xcj.hostpital.manage.service.IHospitalSetService;
import ik.xcj.hostpital.manage.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class HospitalSetServiceImpl implements IHospitalSetService {
    @Autowired
    HospitalSetMapper hospitalSetMapper;

    @Autowired
    IHospitalMapper iHospitalMapper;

    @Autowired
    MD5 md5;

    @Override
    public void insert(HospitalSetAddNewDTO hospitalSetAddNewDTO) {
        int i = hospitalSetMapper.countByHoscode(hospitalSetAddNewDTO.getHoscode());
        if(i > 0){
            String message = "添加医院设置失败，该医院设置已存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int a = iHospitalMapper.countByHoscode(hospitalSetAddNewDTO.getHoscode());
        if(a == 0){
            String message = "添加医院设置失败，该医院不存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        HospitalSet hospitalSet = new HospitalSet();
        BeanUtils.copyProperties(hospitalSetAddNewDTO,hospitalSet);
        /**
         * 使用自定义的md5对联系人姓名以及手机号进行加密，生成sign_key
         */
        String name = hospitalSetAddNewDTO.getName();
        String phone = hospitalSetAddNewDTO.getPhone();
        String s = name + MD5.key + phone;
        String signKey = md5.converMD5(s);
        hospitalSet.setSignKey(signKey);
        log.debug("即将插入数据，参数：{}",hospitalSet);
        int rows = hospitalSetMapper.insert(hospitalSet);
        if(rows != 1){
            String message = "添加相册失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        HospitalSetVO hospitalSetVO = hospitalSetMapper.getStandardById(id);
        if(hospitalSetVO == null){
            String message = "删除医院设置失败，该医院设置不存在";
            throw new ServiceException(ServiceCode.ERR_FORBIDDEN,message);
        }
        hospitalSetMapper.deleteById(id);
    }

    @Override
    public void updateById(Long id, HospitalSetUpdateDTO hospitalSetUpdateDTO) {
        HospitalSetVO hospitalSetVO = hospitalSetMapper.getStandardById(id);
        if (hospitalSetVO == null) {
            String message = "查询失败，数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        HospitalSet hospitalSet = new HospitalSet();
        BeanUtils.copyProperties(hospitalSetUpdateDTO, hospitalSet);
        /**
         * 使用自定义的md5对联系人姓名以及手机号进行加密，生成sign_key
         */
        String name = hospitalSetUpdateDTO.getName();
        String phone = hospitalSetUpdateDTO.getPhone();
        String s = name + MD5.key + phone;
        String signKey = md5.converMD5(s);
        hospitalSet.setSignKey(signKey);
        log.debug("即将插入数据，参数：{}", hospitalSet);
        int rows = hospitalSetMapper.update(hospitalSet);
        if (rows != 1) {
            String message = "添加相册失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public HospitalSetQVO selectById(Long id) {
        HospitalSetVO hospitalSet = hospitalSetMapper.getStandardById(id);
        if(hospitalSet == null){
            String message = "查询失败，数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("参数：{}",hospitalSet);
        String signKey = hospitalSet.getSignKey();
        String s = md5.converMD5(signKey);
        String[] split = s.split(MD5.key);
        log.debug("参数为：{}",split);
        HospitalSetQVO hospitalSetQVO = new HospitalSetQVO();
        BeanUtils.copyProperties(hospitalSet,hospitalSetQVO);
        hospitalSetQVO.setName(split[0]);
        hospitalSetQVO.setPhone(split[1]);
        return hospitalSetQVO;
    }

    @Override
    public PageInfo<HospitalSetQVO> list(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<HospitalSetVO> list = hospitalSetMapper.list();
        List<HospitalSetQVO> hospitalSetQVOList = new ArrayList<>();
        int size = list.size();
        log.debug("集合长度为：{}",size);
        for(int i = 0 ; i < list.size() ; i++){
            HospitalSetVO hospitalSetVO = list.get(i);
            String signKey = hospitalSetVO.getSignKey();
            String s = md5.converMD5(signKey);
            String[] split = s.split(MD5.key);
            HospitalSetQVO hospitalSetQVO = new HospitalSetQVO();
            BeanUtils.copyProperties(hospitalSetVO,hospitalSetQVO);
            hospitalSetQVO.setName(split[0]);
            hospitalSetQVO.setPhone(split[1]);
            HospitalVO hospitalVO = iHospitalMapper.selectByHoscode(hospitalSetVO.getHoscode());
            hospitalSetQVO.setHospitalName(hospitalVO.getHospitalName());
            hospitalSetQVOList.add(hospitalSetQVO);
        }
        return new PageInfo<>(hospitalSetQVOList);
    }
}
